/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package algoritmogeneticoCHC;

import java.io.*;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Zal
 */
public class ZFile {
    private String Path;
    private String FileName;
    private String Extension;
    InputStream     fileInputStream;
    OutputStream    fileOutputStream;
    BufferedReader  bufferReader;
    BufferedWriter  bufferedWriter;
    ZFileMode       Mode;

    public boolean OpenLocal(String file, ZFileMode mode) {
        String fileName = getLocalPath() + file;
        return Open(fileName, mode);
    }
    
    public boolean Open(String file, ZFileMode mode) {
        Mode = mode;
        try {
            if( Mode == ZFileMode.READ) {
                fileInputStream = new FileInputStream(file);
                bufferReader = new BufferedReader(new InputStreamReader(fileInputStream, Charset.forName("UTF-8")));
            }
            else if ( Mode == ZFileMode.WRITE){
                fileOutputStream = new FileOutputStream(file, false);
                bufferedWriter =  new BufferedWriter(new OutputStreamWriter(fileOutputStream, Charset.forName("UTF-8")));
            }
            else if ( Mode == ZFileMode.APPEND){
                fileOutputStream = new FileOutputStream(file, true);
                bufferedWriter =  new BufferedWriter(new OutputStreamWriter(fileOutputStream, Charset.forName("UTF-8")));
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ZFile.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        
        //Obtain data in a reverse find
        char[] charArr = file.toCharArray();
        int dotPos = file.length();
        int bsPos = file.length();
        int seekPos = file.length()-1;
        for (; seekPos >= 0; seekPos--) {
            if( charArr[seekPos] == '.'){
                dotPos = seekPos;
                break;
            }
        }
        for (; seekPos >= 0; seekPos--) {
            if( charArr[seekPos] == '\\') {
                bsPos = seekPos;
                break;
            }
        }
        
        Extension = file.substring(dotPos+1);
        FileName = file.substring(bsPos+1);
        Path = file.substring(0, bsPos+1);
        return true;
    }
    
    public void Close() {
        try {
            //Close file
            if(Mode == ZFileMode.READ)
            {                
                bufferReader.close();
                fileInputStream.close();
            }
            else if(Mode == ZFileMode.WRITE ||
                    Mode == ZFileMode.APPEND){
                bufferedWriter.close();
                fileOutputStream.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(ZFile.class.getName()).log(Level.SEVERE, null, ex);
        }        
    }
    
    public String ReadLine(){
        if( Mode != ZFileMode.READ ) {
            return null;
        }
        try {
            return bufferReader.readLine();
        } catch (IOException ex) {
            Logger.getLogger(ZFile.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public boolean Write(String line) {
        if( Mode != ZFileMode.WRITE &&
            Mode != ZFileMode.APPEND ) {
            return false;
        }
        try {
            bufferedWriter.write(line);
            return true;
        } catch (IOException ex) {
            Logger.getLogger(ZFile.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    
    public boolean WriteLn(String line) {
        if( Mode != ZFileMode.WRITE &&
            Mode != ZFileMode.APPEND ) {
            return false;
        }
        line += "\r\n";
        return Write(line);
    }
    
    public String getLocalPath() {
        String localPath;
        try {
            localPath = new java.io.File(".").getCanonicalPath()+File.separator;
        } catch (IOException ex) {
            localPath = ""; 
        }
        
        return localPath;
    }
        
    public boolean createPath(String path) {
        boolean result = (new File(path)).mkdirs();
        return result;
    }
    
    public boolean createLocalPath(String path) {
        String pathToCreate = "."+File.separator+path;
        boolean result = (new File(pathToCreate)).mkdirs();
        return result;
    }
    
    public String writeCSVLn(double[] arr) {
        if( arr.length < 1 ) {
            return "";
        }
        String result = "";
        
        for (int i = 0; i < arr.length; i++) {
            result += ","+Double.toString(arr[i]);        
        }
        return result;
    }
    
    public String writeCSVLn(int[] arr) {
        if( arr.length < 1 ) {
            return "";
        }
        String result = "";
        
        for (int i = 0; i < arr.length; i++) {
            result += ","+Integer.toString(arr[i]);
        }
        return result;
    }
}
